import {
    createSlice,
    createAsyncThunk
  } from "@reduxjs/toolkit";
  
  import {
    movieRequest
  } from '../../api'
  
  
  //我们使用createAsyncThunk方法来创建异步action
  //createAsyncThunk接受两个参数,第一个参数是type的值,第二个参数是回调函数,回调函数就是异步action的函数
  export const getMovieAction = createAsyncThunk("movie/getMovieList", async () => {
    //在当前函数中执行异步操作
    const result = await movieRequest();
    console.log(result, "result");
    //在这个函数中return的值，就是在thunk中间件中再次disptch的值
    return result.data.data.list
  })
  
  const movieSlice = createSlice({
    name: "movie",
    initialState: {
      movieList: []
    },
    reducers: {
  
    },
    //异步action的处理,需要书写额外的reducer处理,不能写在reduers中,因为reducers会创建一个对应的action
    extraReducers: {
      [getMovieAction.fulfilled](prev, action) {
        console.log("异步action成功--getMovieAction");
        console.log(prev, action);
        prev.movieList = action.payload
      },
      [getMovieAction.rejected]() {
        console.log("异步action失败--getMovieAction");
      },
      [getMovieAction.pending]() {
        console.log("异步action正在进行中--getMovieAction");
      },
    }
  })
  
  export default movieSlice.reducer;